﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmDMTCNX
    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _LaNhap As Boolean
    Public Property LaNhap() As Boolean
        Get
            Return _LaNhap
        End Get
        Set(ByVal value As Boolean)
            _LaNhap = value
        End Set
    End Property

    Private _IDTCNX As String
    Public Property IDTCNX() As String
        Get
            Return _IDTCNX
        End Get
        Set(ByVal value As String)
            _IDTCNX = value
        End Set
    End Property

    Private Sub FrmDMTCNX_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon Then
            If grvTCNX.FocusedRowHandle < 0 Then
                Return
            End If
            IDTCNX = grvTCNX.GetRowCellValue(grvTCNX.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSaoChep.Visibility = IIf(SaoChep, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub

    Private Sub FrmDMTCNX_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnThoat.Caption = BtnExitCaption
        btnSaoChep.Caption = BtnCopyCaption
        btnXuatExcel.Caption = BtnExportDataCaption
        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)
        FillDataToGrid()
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid()
        If LaChon Then
            If LaNhap Then
                grdTCNX.DataSource = (From ds In dbTV.DM_TCNXes Where ds.InUsed And ds.MaTCNX.Substring(0, 1).ToUpper = "N" _
                                      Select New With {.ID = ds.ID, .MaTCNX = ds.MaTCNX, .TenTCNX = ds.TenTCNX}).ToList
                grdTCNX.RefreshDataSource()
            Else
                grdTCNX.DataSource = (From ds In dbTV.DM_TCNXes Where ds.InUsed And ds.MaTCNX.Substring(0, 1).ToUpper = "X" _
                                     Select New With {.ID = ds.ID, .MaTCNX = ds.MaTCNX, .TenTCNX = ds.TenTCNX}).ToList
                grdTCNX.RefreshDataSource()
            End If
        Else
            grdTCNX.DataSource = (From ds In dbTV.DM_TCNXes Where ds.InUsed Select New With { _
                                          .ID = ds.ID, .MaTCNX = ds.MaTCNX, .TenTCNX = ds.TenTCNX}).ToList
            grdTCNX.RefreshDataSource()
        End If

    End Sub
    Private Sub btnSaoChep_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSaoChep.ItemClick
        Dim strID As String = grvTCNX.GetRowCellValue(grvTCNX.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn tính chất nhập xuất")
            Return
        End If
        Dim objCopy = (From ds In dbTV.DM_TCNXes Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objCopy IsNot Nothing Then
            Dim frm As New FrmCapNhatTCNX
            frm.Action = vCopyAction
            frm.TCNXEdit = objCopy
            frm.IDTCNX = strID
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        Else
            MsgInfo("Không tồn tại tính chất nhập xuất bạn chọn")
            Return
        End If
        
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        Dim strID As String = grvTCNX.GetRowCellValue(grvTCNX.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn tính chất nhập xuất")
            Return
        End If
        Dim objCopy = (From ds In dbTV.DM_TCNXes Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objCopy IsNot Nothing Then
            Dim frm As New FrmCapNhatTCNX
            frm.Action = vEditAction
            frm.TCNXEdit = objCopy
            frm.IDTCNX = strID
            frm.StartPosition = FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        Else
            MsgInfo("Không tồn tại tính chất nhập xuất bạn chọn")
            Return
        End If
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        Dim strID As String = grvTCNX.GetRowCellValue(grvTCNX.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn tính chất nhập xuất")
            Return
        End If
        If MsgAskDelete("Bạn có muốn xóa tính chất nhập xuất này không? ") = vMsg.vReSult.Yes Then
            Dim objDelete = (From ds In dbTV.DM_TCNXes Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
            If objDelete IsNot Nothing Then
                objDelete.InUsed = False
                objDelete.EditedOn = dbTV.GetDateTimeServer
                iSubmitChange()
                FillDataToGrid()
            End If
        End If
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdTCNX)
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnChon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChon.ItemClick
        Dim strID As String = grvTCNX.GetRowCellValue(grvTCNX.FocusedRowHandle, grdColID).ToString
        If String.IsNullOrEmpty(strID) Then
            MsgInfo("Bạn chưa chọn tính chất nhập xuất")
            Return
        End If
        IDTCNX = strID
        Me.Close()
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatTCNX
        frm.Action = vAddAction
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.ShowDialog()
        FillDataToGrid()
    End Sub
End Class